package com.lion.appletrip.fragment;


import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

import com.lion.appletrip.R;
import com.lion.appletrip.SettingActivity;
import com.lion.appletrip.utils.AccessTokenKeeper;
import com.lion.appletrip.utils.BmobDataPrint;
import com.lion.appletrip.utils.Constants;
import com.lion.appletrip.utils.PreferenceUtils;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.exception.WeiboException;
import com.sina.weibo.sdk.net.RequestListener;
import com.sina.weibo.sdk.openapi.UsersAPI;
import com.sina.weibo.sdk.openapi.models.ErrorInfo;
import com.sina.weibo.sdk.openapi.models.User;
import com.xinbo.utils.UILUtils;
import com.xinbo.widget.CircleImageView;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * A simple {@link Fragment} subclass. Activities that contain this fragment
 * must implement the {@link Fragment5.OnFragmentInteractionListener} interface
 * to handle interaction events. Use the {@link Fragment5#newInstance} factory
 * method to create an instance of this fragment.
 *
 */
public class Fragment5 extends Fragment
{
	private LayoutInflater inflater;
	private Oauth2AccessToken mAccessToken;
	/** 用户信息接口 */
	private UsersAPI mUsersAPI;	
	private TextView tvNickName;
	
	
	
	public Fragment5()
	{
		
	}



	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	        Bundle savedInstanceState)
	{
	
		this.inflater=inflater;
		View layout = inflater.inflate(R.layout.fragment_fragment5, container, false);
		View headviewUser = inflater.inflate(R.layout.fragment_fragment5_headview_item1, null);
		View diliver = inflater.inflate(R.layout.fragment_fragment5_headview_item2, null);
		ListView mListView= (ListView) layout.findViewById(R.id.listView1);
		tvNickName = (TextView) headviewUser.findViewById(R.id.tv_nickname);
		ivAvatar = (CircleImageView) headviewUser.findViewById(R.id.iv_avatar_headviewuser);
		mAccessToken = AccessTokenKeeper.readAccessToken(getContext());
		if (mAccessToken.isSessionValid())
		{
			mUsersAPI = new UsersAPI(getContext(), Constants.APP_KEY,
			        mAccessToken);
			long uid = Long.parseLong(mAccessToken.getUid());
			mUsersAPI.show(uid, mListener);
		}
		mListView.addHeaderView(headviewUser, null, false);
		mListView.addHeaderView(diliver, null, false);
		MyAdapter adapter= new MyAdapter();
		mListView.setAdapter(adapter);
		layout.findViewById(R.id.btn_settting_fg5).setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				intent.setClass(getContext(), SettingActivity.class);
				startActivity(intent);
			}
		});
		
		return layout;
	}
	@Override
	public void onResume()
	{
	   
	    super.onResume();
	    boolean bomb = PreferenceUtils.getBomb(getContext());
	    if (bomb)
        {
	    	BmobQuery<BmobDataPrint> bmobQuery = new BmobQuery<BmobDataPrint>();
			Log.e("bmobQuery", "bmobQuery:" + bmobQuery.toString());
			bmobQuery.findObjects(getContext(),
			        new FindListener<BmobDataPrint>()
			        {
				        @Override
				        public void onSuccess(List<BmobDataPrint> objects)
				        {
					        if (objects != null && objects.size() > 0)
					        {
						        BmobDataPrint dataPrint = objects.get(objects
						                .size() - 1);
						        String nichen = dataPrint.getNichen();
						        tvNickName.setText(nichen);
					        }
				        }

				        @Override
				        public void onError(int arg0, String arg1)
				        {
				        }

			        });
        }
	    
	    
	}
	/**
	 * 微博 OpenAPI 回调接口。
	 */
	private RequestListener mListener = new RequestListener()
	{
		@Override
		public void onComplete(String response)
		{
			if (!TextUtils.isEmpty(response))
			{

				// 调用 User#parse 将JSON串解析成User对象
				User user = User.parse(response);
				if (user != null)
				{
					/*
					 * Toast.makeText(getContext(), "获取User信息成功，用户昵称：" +
					 * user.screen_name, Toast.LENGTH_LONG).show();
					 */
					tvNickName.setText(user.screen_name);
					UILUtils.displayImageNoAnim(user.profile_image_url,
							ivAvatar);
				} else
				{
					Toast.makeText(getContext(), response, Toast.LENGTH_LONG)
					        .show();
				}
			}
		}

		@Override
		public void onWeiboException(WeiboException e)
		{

			ErrorInfo info = ErrorInfo.parse(e.getMessage());
			Toast.makeText(getContext(), info.toString(), Toast.LENGTH_LONG)
			        .show();
		}
	};
	private CircleImageView ivAvatar;

	class MyAdapter extends BaseAdapter
	{

		@Override
        public int getCount()
        {
	        // TODO Auto-generated method stub
	        return 1;
        }

		@Override
        public Object getItem(int position)
        {
	        // TODO Auto-generated method stub
	        return null;
        }

		@Override
        public long getItemId(int position)
        {
	        // TODO Auto-generated method stub
	        return 0;
        }

		@Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
			View layout = inflater.inflate(R.layout.listview_item_fg5, null);
	        return layout;
        }
		
	}
}
